{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0629e253",
   "metadata": {},
   "source": [
    "# 准备TFLite模型文件\n",
    "\n",
    "同济子豪兄 2023-9-2"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8958e11e",
   "metadata": {},
   "source": [
    "## 第一步：用MMSegmentation，训练PyTorch模型\n",
    "\n",
    "https://github.com/TommyZihao/MMSegmentation_Tutorials\n",
    "\n",
    "## 第二步：用MMDeploy，将PyTorch模型转为ONNX模型\n",
    "\n",
    "https://www.bilibili.com/video/BV1Ep4y1n7wo\n",
    "\n",
    "注意，转换时，使用`静态batch`，而不要使用`动态batch`\n",
    "\n",
    "子豪兄转换好的FastSCNN西瓜语义分割ONNX模型：https://zihao-openmmlab.obs.cn-east-3.myhuaweicloud.com/20230130-mmseg/watermelon/ckpt/mmseg2onnx_fastscnn_static.zip"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9f7187e4",
   "metadata": {},
   "source": [
    "## 第三步：用AIMO模型转换平台，将ONNX模型转为TFLite模型\n",
    "\n",
    "http://aimo.aidlux.com\n",
    "\n",
    "账号：AIMOTC001\n",
    "\n",
    "密码：AIMOTC001"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56ae9118",
   "metadata": {},
   "source": [
    "## 第四步：在Netron中可视化ONNX和TFLite模型结构\n",
    "\n",
    "https://netron.app"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5497f1cf",
   "metadata": {},
   "source": [
    "## 直接下载子豪兄转换好的TFLite模型文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9ed2dacd-0c93-4e25-bc0d-03c43fb8eb37",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 删除原有的模型目录（如有）\n",
    "!rm -rf mmseg2onnx_fastscnn_static_aimo_tflite.zip mmseg2onnx_fastscnn_static_aimo_tflite\n",
    "\n",
    "# 下载压缩包\n",
    "!wget https://zihao-openmmlab.obs.cn-east-3.myhuaweicloud.com/20230130-mmseg/watermelon/ckpt/mmseg2onnx_fastscnn_static_aimo_tflite.zip\n",
    "\n",
    "# 解压\n",
    "!unzip mmseg2onnx_fastscnn_static_aimo_tflite.zip >> /dev/null\n",
    "\n",
    "# 删除压缩包\n",
    "!rm -rf mmseg2onnx_fastscnn_static_aimo_tflite.zip"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b422a46c",
   "metadata": {},
   "source": [
    "## 下载待测图像和视频素材"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "dd83b67f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 西瓜图片和视频，拍摄人：田文博\n",
    "\n",
    "!wget https://zihao-openmmlab.obs.cn-east-3.myhuaweicloud.com/20230130-mmseg/watermelon/data/watermelon_test1.jpg\n",
    "\n",
    "!wget https://zihao-openmmlab.obs.cn-east-3.myhuaweicloud.com/20230130-mmseg/watermelon/data/video_watermelon_3.mov"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4cc8e345",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
